<template>
  <el-dialog
    :title="'计算'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="thisDataForm" :rules="dataRule" ref="dataForm"
             label-width="80px">
      <el-form-item label="数据输入" prop="input">
        <el-input type="textarea"
                  :autosize="{ minRows: 1, maxRows: 10}"
                  v-model="thisDataForm.input" :disabled="disabled" placeholder="数据输入"></el-input>
      </el-form-item>
      <el-form-item label="数据输出" prop="output">
        <el-input type="textarea"
                  :autosize="{ minRows: 1, maxRows: 8}"
                  v-model="thisDataForm.output" :disabled="!disabled" placeholder="数据输出"></el-input>
      </el-form-item>
      <el-form-item label="耗时(ms)" prop="cost">
        <el-input v-model="thisDataForm.cost" :disabled="!disabled" placeholder="算法耗时"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">计算</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          code: '',
          introduce: '',
          inputExplain: '',
          outputExplain: '',
          applicationScenario: '',
          titleLink: '',
          remark: ''
        },
        thisDataForm: {
          input: '',
          output: '',
          cost: ''
        },
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/alg/subject/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.subject
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/alg/subject/cal/${this.dataForm.id}`,
                method: 'post',
                data: this.thisDataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = true
                  this.thisDataForm.cost = data.cost
                  this.thisDataForm.output = data.output
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
